package com.xxl.job.executor.service;

import java.util.ArrayList;
import java.util.Date;

import com.xxl.job.executor.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.executor.kafka.KafkaProduceService;
import com.xxl.job.executor.repository.CurrentInfoRepository;
import com.xxl.job.executor.utils.ThreadLocalDateUtil;

@Service
public class TemperatureSensingOpticalFiberService {

	private static final Logger LOGGER = LoggerFactory.getLogger(AutomaticControlService.class);
	@Autowired
	private SignalDefService signalDefService;
	@Autowired
	private CurrentInfoRepository currentInfoRepository;
	@Autowired
	private KafkaProduceService kafkaProduceService;

	@Autowired
	private BkJsonDataService bkJsonDataService;

	public void save(SignalDefHkModel temperatureSensingOptical,  int flag, ArrayList<DevInfosModel> dataDevModelArrayList) {
		SignalDefXfModel signalDefXfModel = signalDefService.queryXfSignal(temperatureSensingOptical.getGatherId(),
				temperatureSensingOptical.getGatherResId(), "1", "1", temperatureSensingOptical.getPointAddr());
		if (signalDefXfModel == null) {
		//	LOGGER.error("感温光纤系统：未找到对应的信号配置");
			return;
		}
		String nDate = "";
		nDate = ThreadLocalDateUtil.formatDate(new Date());

		// String signalType = signalDefXfModel.getSignalType();

		// 新建实时存储的实体
		CurrentInfoXfModel currentInfoXfModel = new CurrentInfoXfModel();

		currentInfoXfModel.setValue(temperatureSensingOptical.getSignalValue());
		currentInfoXfModel.setGatherResId(temperatureSensingOptical.getGatherResId());
		currentInfoXfModel.setGatherId(temperatureSensingOptical.getGatherId());
		currentInfoXfModel.setDevCode(signalDefXfModel.getDevCode());
		currentInfoXfModel.setSignalCode(signalDefXfModel.getCode());
		currentInfoXfModel.setSignalName(signalDefXfModel.getName());
		currentInfoXfModel.setSignalType(signalDefXfModel.getSignalType());
		currentInfoXfModel.setValunit(signalDefXfModel.getValunit());
		currentInfoXfModel.setTime(nDate);
		currentInfoXfModel.setPointAddr(signalDefXfModel.getPointAddr());

		// 根据实体去redis查询上次信号时间
		CurrentInfoXfModel currentInfoModel1 = currentInfoRepository.queryXfCurrent(signalDefXfModel.getDevCode(),
				signalDefXfModel.getCode());

		if (currentInfoModel1 == null) {
			currentInfoXfModel.setLastTime(nDate);
		} else {
			currentInfoXfModel.setLastTime(currentInfoModel1.getTime());
		}
		currentInfoXfModel.setSendTime(ThreadLocalDateUtil.formatDate(new Date()));
		boolean isSend = this.isSaveRedis(currentInfoXfModel, currentInfoModel1, flag);

		if (isSend){
			// 实时数据存入redis
			currentInfoRepository.save(currentInfoXfModel);
			// 发送kafka
			send(currentInfoXfModel);

//			//发送博坤
//			if(flag==1){
				DevInfosModel devInfosModel = bkJsonDataService.getDataRealTimeMonitorModel( signalDefXfModel.getCode(),  signalDefXfModel.getName(),
						signalDefXfModel.getSignalType(),
						signalDefXfModel.getDevCode(),
						temperatureSensingOptical.getSignalValue(),
						signalDefXfModel.getValunit(),
						nDate);
				dataDevModelArrayList.add(devInfosModel);
//				bkJsonDataService.sendDevInfoMessage(dataDevModelArrayList);
//			}else{
//				DevInfosModel devInfosModel = bkJsonDataService.getDataRealTimeMonitorModel( signalDefXfModel.getCode(),  signalDefXfModel.getName(),
//						signalDefXfModel.getSignalType(),
//						signalDefXfModel.getDevCode(),
//						temperatureSensingOptical.getSignalValue(),
//						signalDefXfModel.getValunit(),
//						nDate);
//				dataDevModelArrayList.add(devInfosModel);
//			}
		}
		//发送博坤
		if(flag==1 && dataDevModelArrayList.size()>0){
			bkJsonDataService.sendDevInfoMessage(dataDevModelArrayList);
		}

	}

	/**
	 * 发kafka
	 * 
	 * @param message
	 */
	public void send(CurrentInfoXfModel message) {

		try {
			this.kafkaProduceService.sendXfAlarmMessage(message);

		} catch (Exception e) {
			XxlJobLogger.log("发送告警信息失败:" + message.toString(), e);
		}
	}


	/**
	 * 判断实时数据是否需要存入redis
	 *
	 * @param automaticControlStatus
	 * @param automaticControlStatus1
	 * @param flag
	 * @return
	 */
	private boolean isSaveRedis(CurrentInfoXfModel automaticControlStatus,
								CurrentInfoXfModel automaticControlStatus1, int flag) {
		int fa = 0;
		long iTimeDiffHour=0;

		if(automaticControlStatus1 != null){
			String autoVal=automaticControlStatus.getValue();
			String oldautoVal=automaticControlStatus1.getValue();
			if(!autoVal.equals(oldautoVal)){
				fa = 1;
			}
			iTimeDiffHour = (ThreadLocalDateUtil.parse(automaticControlStatus.getTime()).getTime() - ThreadLocalDateUtil.parse(automaticControlStatus1.getTime()).getTime()) / (1000 * 60* 20);
		}else{
			iTimeDiffHour = 2;
		}

		if ( fa > 0 || iTimeDiffHour > 1) {
			return true;
		}
		return false;
	}

}
